package com.onLineCar.dispatch.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class QueueConfig {
    public static final String DISPATCH_QUEUE_NAME = "dispatch_queue";
    public static final String DISPATCH_EXCHANGE_NAME = "dispatch_exchange";
    public static final String DISPATCH_ROUTING_KEY = "dispatch_routing_key";
    public static final String DLX_QUEUE_NAME = "dlx_queue_name";
    public static final String DLX_EXCHANGE_NAME = "dlx_exchange_name";
    public static final String DLX_ROUTING_KEY = "dlx_routing_key";

//    /**
//     * 死信队列
//     * @return
//     */
//    @Bean
//    public Queue dlxQueue() {
//        return new Queue(DLX_QUEUE_NAME, true, false, false);
//    }

//    /**
//     * 死信交换机
//     * @return
//     */
//    @Bean
//    public DirectExchange dlxExchange() {
//        return new DirectExchange(DLX_EXCHANGE_NAME, true, false);
//    }

//    /**
//     * 绑定死信队列和死信交换机
//     * @return
//     */
//    @Bean
//    public Binding dlxBinding() {
//        return BindingBuilder.bind(dlxQueue()).to(dlxExchange())
//                .with(DLX_ROUTING_KEY);
//    }

    /**
     * 普通消息队列
     * @return
     */
    @Bean
    public Queue dispatchQueue() {
        Map<String, Object> args = new HashMap<>();
     /*   //设置消息过期时间
        args.put("x-message-ttl", 1000*10);
        //设置死信交换机
        args.put("x-dead-letter-exchange", DLX_EXCHANGE_NAME);
        //设置死信 routing_key
        args.put("x-dead-letter-routing-key", DLX_ROUTING_KEY);*/
        return new Queue(DISPATCH_QUEUE_NAME, true, false, false, args);
    }

    /**
     * 普通交换机
     * @return
     */
    @Bean
    public DirectExchange dispatchExchange() {
        return new DirectExchange(DISPATCH_EXCHANGE_NAME, true, false);
    }

    /**
     * 绑定普通队列和与之对应的交换机
     * @return
     */
    @Bean
    public Binding dispatchBinding() {
        return BindingBuilder.bind(dispatchQueue())
                .to(dispatchExchange())
                .with(DISPATCH_ROUTING_KEY);
    }
}
